home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / TriGrids / Sources / TriGrid3DSupport.c < prev    next >
Encoding:
Text File  |  1997-08-14  |  8.0 KB  |  284 lines  |  [TEXT/MPS ]

  1. // My3dSupport.c - QuickDraw 3d routines
  2. //
  3. // This file contains utility routines for QuickDraw 3d sample code.
  4. //
  5. // Created 27th Dec 1994, Nick Thompson, DEVSUPPORT
  6. //
  7. // Modification History:
  8. //
  9. //    12/27/94        nick        initial version
  10. //    12/31/94        nick        add support for error library routines
  11. //    10/01/95        nick        adjustments for simple box program
  12. //    03/22/95        rdd            added geometry sample code. Updated for B1C2e7
  13. //                                draw context and picking changes.
  14. //                                Press any digit 1 through 9 to select a trigrid
  15. //                                and hold down control to render the trigrid with
  16. //                                texture map.
  17. //    04/12/95        rdd            deleted MyColorBoxFaces and other unused routines.
  18.  
  19. #include <QuickDraw.h>
  20. #include <QDOffScreen.h>
  21.  
  22.  
  23. #include "TriGrid3DSupport.h"
  24. #include "GeometrySample.h"
  25.  
  26.  
  27. #include "QD3DDrawContext.h"
  28. #include "QD3DRenderer.h"
  29. #include "QD3DShader.h"
  30. #include "QD3DCamera.h"
  31. #include "QD3DLight.h"
  32. #include "QD3DGeometry.h"
  33. #include "QD3DGroup.h"
  34. #include "QD3DMath.h"
  35. #include "QD3DSet.h"
  36. #include "QD3DTransform.h"
  37.  
  38.  
  39.  
  40. TQ3ViewObject MyNewView(WindowPtr theWindow)
  41. {
  42.     TQ3Status                myStatus;
  43.     TQ3ViewObject            myView;
  44.     TQ3DrawContextObject    myDrawContext;
  45.     TQ3RendererObject        myRenderer;
  46.     TQ3CameraObject            myCamera;
  47.     TQ3GroupObject            myLights;
  48.     
  49.     myView = Q3View_New();
  50.     
  51.     //    Create and set draw context.
  52.     if ((myDrawContext = MyNewDrawContext(theWindow)) == nil )
  53.         goto bail;
  54.         
  55.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure )
  56.         goto bail;
  57.  
  58.     Q3Object_Dispose( myDrawContext ) ;
  59.     
  60.     //    Create and set renderer.
  61.     
  62.     // this would use the interactive software renderer
  63.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil ) {
  64.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) != kQ3Failure ) {
  65.             Q3InteractiveRenderer_SetDoubleBufferBypass(myRenderer, kQ3True);
  66.         } 
  67.         else {
  68.             goto bail;
  69.         }
  70.     }
  71.     else {
  72.         // this would use the wireframe renderer
  73.         if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame)) != nil ) {
  74.             if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) != kQ3Failure ) {
  75.             } 
  76.             else {
  77.                 goto bail;
  78.             }
  79.         }
  80.     }
  81.  
  82.     Q3Object_Dispose( myRenderer ) ;
  83.     
  84.     //    Create and set camera.
  85.     if ( (myCamera = MyNewCamera(theWindow)) == nil )
  86.         goto bail;
  87.         
  88.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure )
  89.         goto bail;
  90.  
  91.     Q3Object_Dispose( myCamera ) ;
  92.     
  93.     //    Create and set lights.
  94.     if ((myLights = MyNewLights()) == nil )
  95.         goto bail;
  96.         
  97.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure )
  98.         goto bail;
  99.         
  100.     Q3Object_Dispose(myLights);
  101.  
  102.     //    Done!!!
  103.     return ( myView );
  104.     
  105. bail:
  106.     //    If any of the above failed, then don't return a view.
  107.     return ( nil );
  108. }
  109.  
  110. //----------------------------------------------------------------------------------
  111.  
  112. TQ3DrawContextObject MyNewDrawContext(WindowPtr theWindow)
  113. {
  114.     TQ3DrawContextData        myDrawContextData;
  115.     TQ3MacDrawContextData    myMacDrawContextData;
  116.     TQ3ColorRGB                clearColor;
  117.     TQ3DrawContextObject    myDrawContext ;
  118.     
  119.     //    Set the background color.
  120.     Q3ColorRGB_Set(&clearColor, 0.2, 0.0, 1.0);
  121.     
  122.     //    Fill in draw context data.
  123.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  124.     myDrawContextData.clearImageColor.a = 1.0;
  125.     myDrawContextData.clearImageColor.r = clearColor.r;
  126.     myDrawContextData.clearImageColor.g = clearColor.g;
  127.     myDrawContextData.clearImageColor.b = clearColor.b;
  128.     myDrawContextData.paneState = kQ3False;
  129.     myDrawContextData.maskState = kQ3False;
  130.     myDrawContextData.doubleBufferState = kQ3True;
  131.  
  132.     myMacDrawContextData.drawContextData = myDrawContextData;
  133.     
  134.     myMacDrawContextData.window = (CGrafPtr) theWindow;        // this is the window associated with the view
  135.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  136.     myMacDrawContextData.viewPort = nil;
  137.     myMacDrawContextData.grafPort = nil;
  138.     
  139.     //    Create draw context and return it, if it’s nil the caller must handle
  140.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData) ;
  141.  
  142.     return myDrawContext ;
  143. }
  144.  
  145. //----------------------------------------------------------------------------------
  146.  
  147. TQ3CameraObject MyNewCamera(WindowPtr theWindow)
  148. {
  149.     TQ3ViewAngleAspectCameraData    perspectiveData;
  150.     TQ3CameraObject                camera;
  151.     
  152.     TQ3Point3D                     from     = {-5.0, 5.0, 11.0 };
  153.     TQ3Point3D                     to         = { 0.0, 0.0,  0.0 };
  154.     TQ3Vector3D                 up         = { 0.0, 1.0,  0.0 };
  155.  
  156.     float                         fieldOfView = .52359333333;
  157.     float                         hither         =  0.001;
  158.     float                         yon         =  1000;
  159.     
  160.     TQ3Status                    returnVal = kQ3Failure ;
  161.  
  162.  
  163.     perspectiveData.cameraData.placement.cameraLocation     = from;
  164.     perspectiveData.cameraData.placement.pointOfInterest     = to;
  165.     perspectiveData.cameraData.placement.upVector             = up;
  166.  
  167.     perspectiveData.cameraData.range.hither    = hither;
  168.     perspectiveData.cameraData.range.yon     = yon;
  169.  
  170.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  171.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  172.     perspectiveData.cameraData.viewPort.width  = 2.0;
  173.     perspectiveData.cameraData.viewPort.height = 2.0;
  174.     
  175.     perspectiveData.fov                = fieldOfView;
  176.     perspectiveData.aspectRatioXToY    =
  177.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  178.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  179.         
  180.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  181.  
  182.     return camera ;
  183. }
  184.  
  185.  
  186. //----------------------------------------------------------------------------------
  187.  
  188. TQ3GroupObject MyNewLights()
  189. {
  190.     TQ3GroupPosition        myGroupPosition;
  191.     TQ3GroupObject            myLightList;
  192.     TQ3LightData            myLightData;
  193.     TQ3PointLightData        myPointLightData;
  194.     TQ3DirectionalLightData    myDirectionalLightData;
  195.     TQ3LightObject            myAmbientLight, myPointLight, myFillLight;
  196.     TQ3Point3D                pointLocation = { -10.0, 0.0, 10.0 };
  197.     TQ3Vector3D                fillDirection = { 10.0, 0.0, 10.0 };
  198.     TQ3ColorRGB                WhiteLight = { 1.0, 1.0, 1.0 };
  199.     
  200.     //    Set up light data for ambient light.  This light data will be used for point and fill
  201.     //    light also.
  202.  
  203.     myLightData.isOn = kQ3True;
  204.     myLightData.color = WhiteLight;
  205.     
  206.     //    Create ambient light.
  207.     myLightData.brightness = .2;
  208.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  209.     if ( myAmbientLight == nil )
  210.         goto bail;
  211.     
  212.     //    Create point light.
  213.     myLightData.brightness = 1.0;
  214.     myPointLightData.lightData = myLightData;
  215.     myPointLightData.castsShadows = kQ3False;
  216.     myPointLightData.attenuation = kQ3AttenuationTypeNone;
  217.     myPointLightData.location = pointLocation;
  218.     myPointLight = Q3PointLight_New(&myPointLightData);
  219.     if ( myPointLight == nil )
  220.         goto bail;
  221.  
  222.     //    Create fill light.
  223.     myLightData.brightness = .2;
  224.     myDirectionalLightData.lightData = myLightData;
  225.     myDirectionalLightData.castsShadows = kQ3False;
  226.     myDirectionalLightData.direction = fillDirection;
  227.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  228.     if ( myFillLight == nil )
  229.         goto bail;
  230.  
  231.     //    Create light group and add each of the lights into the group.
  232.     myLightList = Q3LightGroup_New();
  233.     if ( myLightList == nil )
  234.         goto bail;
  235.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  236.     if ( myGroupPosition == 0 )
  237.         goto bail;
  238.     myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
  239.     if ( myGroupPosition == 0 )
  240.         goto bail;
  241.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  242.     if ( myGroupPosition == 0 )
  243.         goto bail;
  244.  
  245.     Q3Object_Dispose( myAmbientLight ) ;
  246.     Q3Object_Dispose( myPointLight ) ;
  247.     Q3Object_Dispose( myFillLight ) ;
  248.  
  249.     //    Done!
  250.     return ( myLightList );
  251.     
  252. bail:
  253.     //    If any of the above failed, then return nothing!
  254.     return ( nil );
  255. }
  256.  
  257. //----------------------------------------------------------------------------------
  258. TQ3GroupObject MyNewModel()
  259. {
  260.     TQ3GroupObject            myGroup = NULL;
  261.     TQ3ShaderObject            myIlluminationShader;
  262.  
  263.     // Create a group for the complete model.
  264.     // do not use Q3OrderedDisplayGroup_New since in this
  265.     // type of group all of the translations are applied before
  266.     // the objects in the group are drawn, in this instance we 
  267.     // dont want this.
  268.     if ((myGroup = Q3DisplayGroup_New()) != NULL ) {
  269.             
  270.         // Define a shading type for the group
  271.         // and add the shader to the group
  272.         myIlluminationShader = Q3PhongIllumination_New();
  273.         Q3Group_AddObject(myGroup, myIlluminationShader);
  274.     }
  275.     
  276.     // dispose of the objects we created here
  277.     if( myIlluminationShader ) 
  278.         Q3Object_Dispose(myIlluminationShader);    
  279.  
  280.     //    Done!
  281.     return ( myGroup );
  282. }
  283.  
  284.